package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.cache.redis.service.IVisitVoService;
import cn.wolfcode.wolf2w.cache.redis.vo.VisitVO;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.domain.Visit;
import cn.wolfcode.wolf2w.query.VisitQuery;
import cn.wolfcode.wolf2w.repository.VisitRepository;
import cn.wolfcode.wolf2w.service.IUserInfoService;
import cn.wolfcode.wolf2w.service.IVisitService;
import cn.wolfcode.wolf2w.uitl.DBHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class VisitServiceImpl implements IVisitService {

    @Autowired
    private VisitRepository visitepository;
    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private IUserInfoService userInfoService;
    @Autowired
    private IVisitVoService visitVoService;
    @Override
    public void save(Visit visit) {
        visit.setId(null);
        visitepository.save(visit);
    }

    @Override
    public void delete(String id) {
        visitepository.deleteById(id);
    }

    @Override
    public void update(Visit visit) {
        visitepository.save(visit);
    }

    @Override
    public Visit get(String id) {
        return visitepository.findById(id).orElse(null);
    }

    @Override
    public List<Visit> lists() {
        return visitepository.findAll();
    }
    //分页查询
    @Override
    public Page<Visit> query(VisitQuery qo) {
        //创建查询对象
        Query query = new Query();
        //通过查询拿到总条数
        long count = mongoTemplate.count(query, Visit.class);
        //判断有没有数据
        if (count==0){
            return Page.empty();//返回一个空,页面不会错,如果直接返回一个0页面就会报错
        }
        //无条件分页
        Pageable request = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(), Sort.Direction.ASC, "_id");
        //查询所有数据
        return DBHelper.query(mongoTemplate,Visit.class,query,request);
    }

    @Override
    public Map<String, Object> getMap(String wid, String uid) {
        Map<String, Object> map=new HashMap<>();
        //查询我的窝
        UserInfo winfo = userInfoService.get(wid);
        map.put("owner",winfo);
        //定义一个用户集合
        List<UserInfo> ulist=new ArrayList<>();
        Visit visit=visitepository.findByOwnerId(wid);
        List<String> ids = visit.getVisitorIds();
        if (wid.equals(uid)){
            //表示查看自己的我
            if (ids !=null){
                for (String usid : ids) {
                    UserInfo info = userInfoService.get(usid);
                    ulist.add(info);
                }
                //查询两个数量
                map.put("totalView",ids.size());
                VisitVO vo=visitVoService.getVistalVO(wid);
                map.put("todayView",vo.getTodayView());
            }else {
                map.put("totalView",0);
                map.put("todayView",0);
            }

        }else {
            //表示查看别人的窝的我
            ids.add(uid);
            for (String usid : ids) {
                UserInfo info = userInfoService.get(usid);
                ulist.add(info);
            }
            //查询两个数量
            map.put("totalView",ids.size());
            VisitVO vo=visitVoService.getVistalVOday(wid);
            map.put("todayView",vo.getTodayView());
        }
        visit.setVisitorIds(ids);
        visit.setTotalView(ids.size());
        this.update(visit);
        if (ulist.size()>8){
            map.put("visitors",ulist.subList(0, 8));
        }else {
            map.put("visitors",ulist);
        }
        return map;
    }

}
